package at.technikum.fotoalbum.beans;

import java.util.ArrayList;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import at.technikum.fotoalbum.database.DatabaseConnector;
import at.technikum.fotoalbum.utils.PasswordGenerator;
import at.technikum.fotoalbum.utils.Utils;

public class Person {

	private long id;
	private String nick;
	private String password;
	private String password2;
	private String vorname;
	private String nachname;
	private String email;
	private boolean isAdmin;
	private boolean isConfirmed;
	private ArrayList<Person> allPerson;

	public Person() {
	}

	public Person(long id, String nick, String password, String vorname, String nachname, String email,
			boolean isAdmin, boolean isConfirmed) {
		this.id = id;
		this.nick = nick;
		this.password = password;
		this.vorname = vorname;
		this.nachname = nachname;
		this.email = email;
		this.isAdmin = isAdmin;
		this.isConfirmed = isConfirmed;

	}

	public ArrayList<Person> getAllPerson() {
		return allPerson;
	}

	public void setAllPerson(ArrayList<Person> allPerson) {
		this.allPerson = allPerson;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(long id) {
		this.id = id;
	}

	/**
	 * @return the id
	 */
	public long getId() {
		return id;
	}

	public boolean isAdmin() {
		return isAdmin;
	}

	public void setAdmin(boolean isAdmin) {
		this.isAdmin = isAdmin;
	}

	public String getPassword2() {
		return password2;
	}

	public void setPassword2(String password2) {
		this.password2 = password2;
	}

	public String getVorname() {
		return vorname;
	}

	public void setVorname(String vorname) {
		this.vorname = vorname;
	}

	public String getNachname() {
		return nachname;
	}

	public void setNachname(String nachname) {
		this.nachname = nachname;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getNick() {
		return nick;
	}

	public void setNick(String nick) {
		this.nick = nick;
	}

	public String doLogin() {
		SessionBean sb = (SessionBean) Utils.getManagedBean("sessionBean");
		if (getNick() != null && getPassword() != null && checkpassword()) {
			sb.setLoggedIn(true);
			sb.setNick(this.getNick());
			return "/pages/userHome.xhtml";
		} else {
			FacesContext.getCurrentInstance().addMessage(null,
					new FacesMessage(FacesMessage.SEVERITY_ERROR, "Login fehlgeschlagen", null));
			sb.setLoggedIn(false);
			sb.setNick(null);
			sb.setIsAdmin(false);
			return "/pages/login.xhmtl";

		}
	}

	public boolean checkpassword() {
		DatabaseConnector dc = (DatabaseConnector) Utils.getManagedBean("databaseConnector");
		Person person = dc.getUserFromUsername(this.nick);
		if (person.isConfirmed && dc.checkLoginUser(this.nick, this.password)) {
			SessionBean sb = (SessionBean) Utils.getManagedBean("sessionBean");
			sb.setIsAdmin(person.isAdmin);
			return true;
		}
		return false;

	}

	public String generateNewPassword() {
		DatabaseConnector dc = (DatabaseConnector) Utils.getManagedBean("databaseConnector");
		Person person = dc.getUserFromUsername(this.nick);
		if (person != null && person.email != null && person.nick != null && person.nick.equals(this.nick)
				&& person.email.equals(this.email)) {
			String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
			password = PasswordGenerator.generate(alphabet, 5);
			setPassword(password);
			dc.changePassword(this.nick, password);
			return "/pages/showNewPassword.xhtml";
		}
		FacesContext.getCurrentInstance().addMessage(null,
				new FacesMessage(FacesMessage.SEVERITY_ERROR, "Nickname und Mailadresse stimmen nicht überein", null));
		return null;

	}

	public String doRegister() {
		DatabaseConnector dc = (DatabaseConnector) Utils.getManagedBean("databaseConnector");
		if (!dc.existsUser(this.nick)) {
			dc.createNewUser(new Person(this.id, this.nick, this.password, this.vorname, this.nachname, this.email,
					this.isAdmin, this.isConfirmed));
			((SessionBean) Utils.getManagedBean("sessionBean")).setRegisternick(this.getNick());
			return "/pages/registerLink.xhtml";
		}
		FacesContext.getCurrentInstance().addMessage(
				null,
				new FacesMessage(FacesMessage.SEVERITY_ERROR,
						"Nickname existiert bereits, wählen sie bitte einen anderen", null));
		return null;

	}

	public String endRegister() {
		SessionBean sb = (SessionBean) Utils.getManagedBean("sessionBean");
		if (sb.getRegisternick() != null) {
			DatabaseConnector dc = (DatabaseConnector) Utils.getManagedBean("databaseConnector");
			dc.confirmUser(sb.getRegisternick());
		}
		return "/pages/login.xhtml";
	}

	public String loadAllPersons() {
		DatabaseConnector dc = (DatabaseConnector) Utils.getManagedBean("databaseConnector");
		this.setAllPerson(dc.getAllUsers());
		return null;
	}

	/**
	 * @param isConfirmed
	 *            the isConfirmed to set
	 */
	public void setConfirmed(boolean isConfirmed) {
		this.isConfirmed = isConfirmed;
	}

	/**
	 * @return the isConfirmed
	 */
	public boolean isConfirmed() {
		return isConfirmed;
	}

	public String editUser() {
		FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("userEditNick");
		DatabaseConnector dc = (DatabaseConnector) Utils.getManagedBean("databaseConnector");
		Person editPerson = dc.getUserFromUsername(FacesContext.getCurrentInstance().getExternalContext()
				.getRequestParameterMap().get("userEditNick"));
		this.email = editPerson.getEmail();
		this.vorname = editPerson.getVorname();
		this.nachname = editPerson.getNachname();
		this.password = editPerson.getPassword();
		this.nick = editPerson.getNick();
		this.id = editPerson.getId();
		this.isConfirmed = editPerson.isConfirmed;
		this.isAdmin = editPerson.isAdmin;
		return "/pages/adminUserDetail.xhtml";
	}
	
	public String saveEditUser(){
		DatabaseConnector dc = (DatabaseConnector) Utils.getManagedBean("databaseConnector");
		dc.editUser(this);
		return "/pages/adminUserList.xhmtl";
	}
}